(function () {
    window.Block = class Block {
        constructor (options) {
            this.width = 64
            this.height = 32
            this.row = options.row
            this.col = options.col
            this.x = 70 + (this.col - 1) * this.width
            this.y = 70 + (this.row - 1) * this.height
            this.color = options.color
            this.type = options.color
        }
        update(){
           if (!game.ball) return
            // 球撞到上下边
            if (game.ball.x + game.ball.width / 2 >= this.x && game.ball.x + game.ball.width / 2 <= this.x + this.width) {
                if (game.ball.y <= this.y + this.height && game.ball.y + game.ball.height >= this.y) {
                    if (game.ball.type === 'normal') {
                        game.ball.angle = -game.ball.angle
                    }
                    game.blockManger.realMap[this.row - 1][this.col - 1] = null
                    game.blockManger.blockNum --
                    console.log(game.blockManger.blockNum)
                    game.ball.reward(this.type)
                }
            }
            // 撞到左右边 
            else if (game.ball.y + game.ball.height >= this.y && game.ball.y <= this.y + this.height) {
                if (game.ball.x + game.ball.width >= this.x && game.ball.x <= this.x + this.width) {
                    if (game.ball.type === 'normal') {
                        game.ball.angle = 180 - game.ball.angle
                    }
                    game.blockManger.realMap[this.row - 1][this.col - 1] = null
                    game.blockManger.blockNum --
                    console.log(game.blockManger.blockNum)
                    game.ball.reward(this.type)
                }
            }
            if (game.blockManger.blockNum <= 0) {
                game.reset()
            }
        }
        
        render () {
            game.context.drawImage(game.images[`block${this.color}`], 0,0,this.width,this.height,70 + (this.col - 1) * this.width,70 + (this.row - 1) * this.height,this.width,this.height)
        }
    }
})()